package com.serialcommunication.helper;

import java.util.List;

import org.apache.soap.encoding.Hex;
import static com.serialcommunication.constants.ActionConstants.*;

import com.serialcommunication.constants.ProtocolConstants;
import com.serialcommunication.interfaces.IAction;
import com.serialcommunication.model.DynamicAction;

public class ActionUtil {
	
	public static  IAction getActionByName(String pName) {
		IAction action = null;
		for (IAction a : BINARY_ACTION_LIST) {
			if (a.getName().toLowerCase().equals(pName.toLowerCase())) {
				return action;
//				break;
			}
		}
		for (IAction a : ACTION_LIST) {
			if (a.getName().toLowerCase().equals(pName.toLowerCase())) {
				return action;
//				break;
			}
		}
		return null; // should not be reached
	}
	
	/**
	 * If byte is received to identify Action by command Hex and to be able to modify right component in GUI
	 * @param name
	 * @return
	 */
	public static IAction getActionByCommand(String pCommand) {
		IAction action = null;
		if (pCommand.equals(ProtocolConstants.BINARY_COMMAND_INCOMMING)) {
			for (IAction a : BINARY_ACTION_LIST) {
				if (a.getCommand().equals(pCommand)) {
					action = a;
					break;
				}
			}
		} else {
			for (IAction a : ACTION_LIST) {
				if (a.getCommand().equals(pCommand)) {
					action = a;
					break;
				}
			}
		}
		return action;
	}
	
	// not used yet
	public static byte[] hexStringToByteArray(String s) {
	    int len = s.length();
	    byte[] data = new byte[len / 2];
	    for (int i = 0; i < len; i += 2) {
	        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
	                             + Character.digit(s.charAt(i+1), 16));
	    }
	    return data;
	}
}
